package com.simdo.g768;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.graphics.Color;
import android.os.Binder;
import android.os.IBinder;

import androidx.core.app.NotificationCompat;

import com.g768.Logcat.SdkLog;
import com.g768.MessageControl.MessageController;
import com.g768.MessageControl.MessageTransceiver;
import com.g768.Socket.UdpControl;
import com.g768.Socket.ZTcpService;

public class MainService extends Service {
    private ZTcpService mZTcpService;
    private final int NOTIFICATION_ID = 1;

    private final IBinder mBinder = new LocalBinder();

    // 当客户端连接时，这个方法被调用
    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    public class LocalBinder extends Binder {
        public MainService getService() {
            return MainService.this;
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        SdkLog.I("MainService onCreate");
        initSocket();
    }

    /**
     * 绑定业务操作类
     */
    private void initSocket() {
        // Socket连接
        mZTcpService = ZTcpService.build();
        // 回传数据解析类
        MessageTransceiver mTransceiver = MessageTransceiver.build();
        mZTcpService.registerHandler(mTransceiver);
        mTransceiver.setClient(mZTcpService);
        mZTcpService.launch();
        UdpControl.build().startUdp();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        showForegroundNotification();
        return START_STICKY;
    }

    private void showForegroundNotification() {
        // 创建通知通道（如果API级别支持）
        NotificationChannel channel = null;
        String CHANNEL_ID = "foreground_service_channel";
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            channel = new NotificationChannel(CHANNEL_ID,
                    "Foreground Service Channel", NotificationManager.IMPORTANCE_DEFAULT);
            channel.enableLights(false);
            channel.enableVibration(false);
            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            notificationManager.createNotificationChannel(channel);
        }

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle(getString(R.string.app_name))
                .setContentText("AUTO使用服务中")
                .setSmallIcon(R.mipmap.app_icon) // 替换为您的通知图标资源ID
                .setChannelId(CHANNEL_ID)
                .setColor(Color.BLUE); // 设置通知栏的颜色，API 21以上有效

        Intent notificationIntent = new Intent(this, MainActivity.class);
        // 通过PendingIntent打开应用或执行某个动作
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE);
        builder.setContentIntent(pendingIntent);

        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        // 开始前台服务并显示通知
        startForeground(NOTIFICATION_ID, builder.build());
        if (channel != null) manager.createNotificationChannel(channel);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        SdkLog.I("MainService onDestroy");

        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.cancel(NOTIFICATION_ID);
        stopForeground(true);

        // 结束UDP监听广播
        UdpControl.build().closeUdp();
        MessageController.build().close();
        mZTcpService.onDestroy();
    }
}
